package main.栈;

import java.util.Stack;

/**
 * @author tmh
 * @date 2024/7/25 15:55
 * @description
 */
public class T155最小栈 {
    //用两个栈来寻找最小元素
    //stack用来常规存储元素
    Stack<Integer> stack;
    //minStack用来存储最小元素
    Stack<Integer> minStack;

    public T155最小栈() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }

    public void push(int val) {
        //条件符合的话 两个栈里面都放
        if (minStack.isEmpty() || val <= minStack.peek()) {
            minStack.push(val);
            stack.push(val);
        }else {
            stack.push(val);
        }
    }

    public void pop() {
        //判断minStack的栈顶元素和stack的栈顶元素相同不，相同就都pop，不相同就只pop stack的
        if (stack.peek().equals(minStack.peek())) {
            minStack.pop();
            stack.pop();
        } else {
            stack.pop();
        }
    }

    public int top() {
        return stack.peek();
    }

    public int getMin() {
        return minStack.peek();
    }
}
